package com.sunday.shoufen.ui;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.shoufen.R;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.entity.Fans;
import com.sunday.shoufen.http.ApiClient;
import com.sunday.shoufen.ui.widgets.AddMemberWindow;
import com.sunday.shoufen.ui.widgets.GiveCouponWindow;
import com.sunday.shoufen.ui.widgets.MassGiveCouponWindow;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.shoufen.base.Constants.CODE_1;
import static com.sunday.shoufen.base.Constants.SEND_CODE_SIGN;


/**
 * Created by 刘涛 on 2017/11/8.
 */

public class SearchMemberActivity extends BaseActivity {

    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.edit_name)
    EditText editName;
    @Bind(R.id.ll_search)
    LinearLayout llSearch;
    @Bind(R.id.ll_not_found)
    LinearLayout llNotFound;

    private long memberId;
    private String fanName;
    private long tickerId;
    private int type;//0:单张赠送卡券 1：整体赠送卡券 2：添加会员

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_member);
        ButterKnife.bind(this);
        initView();

    }

    private void initView() {
        tickerId = getIntent().getLongExtra("ticketId", 0);
        type = getIntent().getIntExtra("type", 0);
        switch (type) {
            case 0:
            case 1:
                titleView.setText("赠送卡券");
                break;
            case 2:
                titleView.setText("添加会员");
                break;
        }

        memberId = BaseApp.getInstance().getMember().getId();
        editName.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        editName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH
                        || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    fanName = editName.getText().toString().trim();
                    if (TextUtils.isEmpty(fanName)) {
                        return true;
                    }
                    /*隐藏软键盘*/
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    }
                    getMemberInfo();
                    return true;
                } else {
                    return false;
                }
            }

        });
    }

    private Fans fans;

    private void getMemberInfo() {
        //先调用查询
        showLoadingDialog(0);
        Call<ResultDO<Fans>> call = ApiClient.getApiAdapter().findMember(fanName);
        call.enqueue(new Callback<ResultDO<Fans>>() {
            @Override
            public void onResponse(Call<ResultDO<Fans>> call, final Response<ResultDO<Fans>> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {

                    llSearch.setVisibility(View.VISIBLE);
                    llNotFound.setVisibility(View.GONE);
                    TextView textView = (TextView) findViewById(R.id.fans_name1);
                    textView.setText(fanName);
                    fans = response.body().getResult();
                    llSearch.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            switch (type) {
                                case 0:
                                    showGiveDialog();
                                    break;
                                case 1:
                                    showMassGiveDialog();
                                    break;
                                case 2:
                                    showAddDialog();
                                    break;
                            }
                        }
                    });
                } else if (response.body().getCode() == -2) {
                    llSearch.setVisibility(View.GONE);
                    llNotFound.setVisibility(View.VISIBLE);
                    TextView textView = (TextView) findViewById(R.id.fans_name2);
                    textView.setText("该会员不存在");
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Fans>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


    private void showGiveDialog() {
        GiveCouponWindow shareWindow = new GiveCouponWindow(mContext, fans, tickerId);
        shareWindow.showPopupWindow(editName);
    }


    //添加会员
    private void showAddDialog() {
        AddMemberWindow shareWindow = new AddMemberWindow(mContext, fans);
        shareWindow.showPopupWindow(editName);
    }

    //批量转赠卡券
    private void showMassGiveDialog() {
        MassGiveCouponWindow shareWindow = new MassGiveCouponWindow(mContext, fans);
        shareWindow.showPopupWindow(editName);
    }


}
